package cc.douquan.com.doutu.view.activity;

import android.os.Handler;
import android.view.MenuItem;
import android.view.View;

import com.orhanobut.logger.Logger;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import cc.douquan.com.doutu.R;
import cc.douquan.com.doutu.adapter.CollectionAdapter;
import cc.douquan.com.doutu.delegate.MycollectionDelegate;
import cc.douquan.com.doutu.delegate.SwipeRefreshAndLoadMoreCallBack;
import cc.douquan.com.doutu.entity.PicPath;
import cc.douquan.com.doutu.mvp_frame.presenter.ActivityPresenter;
import cc.douquan.com.doutu.utils.CollectionDialog;
import cc.douquan.com.doutu.utils.ShareUtils;
import cc.douquan.com.doutu.utils.ToastUtils;

import static com.umeng.socialize.utils.DeviceConfig.context;

/**
 * Created by feq on 2016/11/2.
 */

public class MycollectionActivity extends ActivityPresenter<MycollectionDelegate> implements SwipeRefreshAndLoadMoreCallBack {
    private static final String DB_NAME = "pic_path.db";
    private static final int DB_VERSION = 1;
    private List<PicPath> mAllCollection;
    private int startNum = 0;
    private int endNum = 20;
    private CollectionAdapter mAdapter;

    @Override
    protected Class<MycollectionDelegate> getDelegateClass() {
        return MycollectionDelegate.class;
    }

    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setToolbar(getSupportActionBar(), "我的收藏");
        mAllCollection = viewDelegate.getAllCollection(DB_NAME, DB_VERSION, startNum + "," + endNum);
        mAdapter = new CollectionAdapter(mAllCollection, this);
        viewDelegate.setListAdapter(mAdapter);
        viewDelegate.registerLoadMoreCallBack(this, mAdapter);
        mAdapter.setOnImageClickListener(new CollectionAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                v = v.findViewById(R.id.iv_img);
                ShareUtils.shareGirl(v, mContext);
            }

            @Override
            public void onItemLongClick(View view, final int position) {
                view = view.findViewById(R.id.iv_img);
                Logger.i("picpath", mAllCollection.get(position).getPicPath());
                CollectionDialog.showDialog(mContext, view, mAllCollection.get(position),
                        new CollectionDialog.OnDeleteStateListener() {
                            @Override
                            public void onState(String state) {
                                mAllCollection.remove(position);
                                mAdapter.notifyDataSetChanged();
                                ToastUtils.showShort(state);
                            }
                        });
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    @Override
    public void refresh() {
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                viewDelegate.setNoRefresh();
            }
        }, 2000);

    }

    @Override
    public void loadMore() {
        startNum += 20;
        endNum += 20;
        List<PicPath> allCollection = viewDelegate.getAllCollection(DB_NAME, DB_VERSION, startNum + "," + endNum);
        if (allCollection.size() > 0) {
            mAllCollection.addAll(allCollection);
            mAdapter.notifyDataSetChanged();
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
